package com.channelsoft.cm.application;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Toast;

/**
 * 登录Activity
 * 
 */
public class LoginActivity extends Activity {
	private EditText edit_userno;
	private EditText edit_userpassword;
	private CheckBox check_issavepassword;
	private CheckBox check_isautologin;
	private SharedPreferences preferences;
	private Intent intent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.login);
		intent = new Intent();
		edit_userno = (EditText) this.findViewById(R.id.edit_userno);
		edit_userpassword = (EditText) this
				.findViewById(R.id.edit_userpassword);
		check_issavepassword = (CheckBox) this
				.findViewById(R.id.check_issavepassword);
		check_isautologin = (CheckBox) this
				.findViewById(R.id.check_isautologin);
		preferences = getSharedPreferences("login", Context.MODE_PRIVATE);
		// 判断是否有密码和账户被保存
		if (preferences.getBoolean("savePassword", false)) {
			String userName = preferences.getString("username", "");
			String passWord = preferences.getString("password", "");
			edit_userno.setText(userName);
			edit_userpassword.setText(passWord);
			check_issavepassword.setChecked(true);
		}
		if (preferences.getBoolean("autoLogin", false)) {
			if (!"".equals(edit_userno.getText().toString())
					& !"".equals(edit_userpassword.getText().toString())) {
				check_isautologin.setChecked(true);
				intent.setClass(LoginActivity.this, HomeActivity.class);
				startActivity(intent);
				LoginActivity.this.finish();
			}
		}

		check_isautologin
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (isChecked) {
							check_issavepassword.setChecked(isChecked);
						}
					}
				});

	}

	/**
	 * 登录按钮响应
	 * 
	 * @param v
	 */
	public void login(View v) {
		String userNo = this.edit_userno.getText().toString();
		String userPassword = this.edit_userpassword.getText().toString();

		if (userNo == null || "".equals(userNo)) {
			Toast.makeText(this, "用户名不能为空", Toast.LENGTH_LONG).show();
		} else if (userPassword == null || "".equals(userPassword)) {
			Toast.makeText(this, "密码不能为空", Toast.LENGTH_LONG).show();
		} else {
			// 进入用户名密码校验
			if ("123456".equals(userNo) && "123456".equals(userPassword)) {
				Toast.makeText(this, "登录成功", Toast.LENGTH_LONG).show();
				// 等待实现-登录成功后判断是否记住用户名或者是设置自动登录, 并进行相应的数据更改
				// 跳转到业务界面 结束登录Activity
				// 判断记住密码是否被选中,选中即保存在shared中
				if (check_issavepassword.isChecked()) {
					Editor editor = preferences.edit();
					editor.putString("username", edit_userno.getText()
							.toString());
					editor.putString("password", edit_userpassword.getText()
							.toString());
					editor.putBoolean("savePassword", true);
					editor.commit();
				} else {
					// 未选中，则查看shared中有无数据，有则删除
					Editor editor = preferences.edit();
					String userName = preferences.getString("username", "");
					String password = preferences.getString("password", "");
					if (userName != null && password != null) {
						editor.putString("username", null);
						editor.putString("password", null);
						editor.putBoolean("autoLogin", false);
						editor.putBoolean("savePassword", false);
						editor.commit();
					}
				}
				// 判断自动登录是否被选中,选中即保存在shared中
				if (check_isautologin.isChecked()) {
					Editor editor = preferences.edit();
					editor.putBoolean("autoLogin", true);
					editor.commit();
				}
				intent.setClass(LoginActivity.this, HomeActivity.class);
				startActivity(intent);
				LoginActivity.this.finish();
			} else {
				Toast.makeText(this, "登录错误,请检查后重新输入", Toast.LENGTH_LONG).show();
			}
		}

	}

}
